/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bddfirstproject.view;

import bddfirstproject.MainController;
import bddfirstproject.controller.AuteurDAL;
import bddfirstproject.controller.ThemeDAL;
import bddfirstproject.model.Auteur;
import bddfirstproject.view.renderer.ComboBoxAuteurRenderer;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;

/**
 *
 * @author epulapp
 */
public class ConnectionView extends NamedJPanel {
    
    protected static final String title = "Selection profil";
    
    /**
     * Creates new form ConnectionView
     */
    public ConnectionView() {
        initComponents();
        this.setSize(800, 600);
        labelErrors.setVisible(false);
        comboBoxAuteurList.setRenderer(new ComboBoxAuteurRenderer());
        ArrayList<Auteur> listeAuteur;
        try {
            listeAuteur = AuteurDAL.getAllAuteur();
            for(Auteur auteur : listeAuteur) {
                comboBoxAuteurList.addItem(auteur);
            }
        } catch (IOException e1) {
            System.out.println("Erreur d'accès à la base " + e1);
            labelErrors.setText("Acces à la base impossible " + e1);
            labelErrors.setVisible(true);
        } catch (java.sql.SQLException e2) {
            System.out.println("Erreur lors de l'exécution de la requête " + e2);
            labelErrors.setText("Erreur d'accès à la base, impossible d'executer la requete");
            labelErrors.setVisible(true);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        ViewTitle = new javax.swing.JLabel();
        comboBoxAuteurList = new javax.swing.JComboBox();
        comboBoxAuteurListLabel = new javax.swing.JLabel();
        buttonValid = new javax.swing.JButton();
        buttonExit = new javax.swing.JButton();
        labelErrors = new javax.swing.JLabel();

        setPreferredSize(new java.awt.Dimension(800, 600));

        ViewTitle.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        ViewTitle.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        ViewTitle.setText("Bienvenue sur l'application Polytech News Writer");

        comboBoxAuteurList.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N

        comboBoxAuteurListLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        comboBoxAuteurListLabel.setText("Selectionner votre profil : ");

        buttonValid.setText("Selectionner ce profil");
        buttonValid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonValidActionPerformed(evt);
            }
        });

        buttonExit.setText("Quitter");
        buttonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonExitActionPerformed(evt);
            }
        });

        labelErrors.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelErrors.setForeground(new java.awt.Color(255, 51, 0));
        labelErrors.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labelErrors.setText("jLabel1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(ViewTitle, javax.swing.GroupLayout.DEFAULT_SIZE, 611, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(comboBoxAuteurListLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(buttonExit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(buttonValid, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 565, Short.MAX_VALUE)
                            .addComponent(comboBoxAuteurList, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(labelErrors, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ViewTitle)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboBoxAuteurList, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboBoxAuteurListLabel))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(buttonValid, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(buttonExit, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(labelErrors)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonValidActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonValidActionPerformed
        if(comboBoxAuteurList.getItemCount() != 0) {
            if(comboBoxAuteurList.getSelectedItem() instanceof Auteur) {
                MainController mainController = MainController.getInstance();
                mainController.setCurrentAuteur((Auteur)comboBoxAuteurList.getSelectedItem());
                mainController.setCurrentView(new ThemeListView());
            }
        }
    }//GEN-LAST:event_buttonValidActionPerformed

    private void buttonExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonExitActionPerformed
        MainController.getInstance().exit();
    }//GEN-LAST:event_buttonExitActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ViewTitle;
    private javax.swing.JButton buttonExit;
    private javax.swing.JButton buttonValid;
    private javax.swing.JComboBox comboBoxAuteurList;
    private javax.swing.JLabel comboBoxAuteurListLabel;
    private javax.swing.JLabel labelErrors;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getTitle() {
        return title;
    }
}
